package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.api.service.IProductService;
import com.dhcc.bpm.modules.common.service.IPluploadService;
import com.dhcc.bpm.modules.content.entity.ProvideProduct;
import com.dhcc.bpm.modules.content.service.IProvideProductService;
import com.dhcc.bpm.modules.content.vo.ProvideProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName ProvideProductController
 * @Description  提供产品表 控制器
 * @Author daina
 * @Date 2020/3/9 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-content/provideproduct")
@Api(value = "提供产品表", tags = "提供产品表接口")
public class ProvideProductController extends BladeController {

	private IProvideProductService provideProductService;
	@Autowired
	private IProductService productService;

	
	private IPluploadService pluploadService;
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param provideProduct
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.content.entity.ProvideProduct>
	 **/
	@ApiLog("提供产品模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入provideProduct")
	@Log(title = "详情", businessType = BusinessType.SELECT)
	public R<ProvideProductVO> detail(ProvideProduct provideProduct) {
		ProvideProduct detail = provideProductService.getDetailById(provideProduct.getId());
		ProvideProductVO productVO = new ProvideProductVO();
		BeanUtils.copyProperties(detail, productVO);
		String productId = productService.findByProductCode(detail.getProductCode());
		productVO.setOpcProductId(productId);
		return R.data(productVO);
	}

	/**
	 * @Description 分页 提供产品表
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param provideProduct
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.content.entity.ProvideProduct>>
	 **/
	@ApiLog("提供产品模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入provideProduct")
	@Log(title = "分页", businessType = BusinessType.SELECT)
	public R<IPage<ProvideProduct>> list(ProvideProduct provideProduct, Query query) {
		IPage<ProvideProduct> pages = provideProductService.page(Condition.getPage(query), Condition.getQueryWrapper(provideProduct));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 提供产品表
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param provideProduct
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.content.vo.ProvideProductVO>>
	 **/
	@ApiLog("提供产品模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入provideProduct")
	@Log(title = "分页", businessType = BusinessType.SELECT)
	public R<IPage<ProvideProductVO>> page(ProvideProductVO provideProduct, Query query) {
		IPage<ProvideProductVO> pages = provideProductService.selectProvideProductPage(Condition.getPage(query), provideProduct);
		return R.data(pages);
	}

	/**
	 * @Description 新增 提供产品表
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param provideProduct
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("提供产品模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入provideProduct")
	@Log(title = "新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody ProvideProduct provideProduct) {
		return R.status(provideProductService.save(provideProduct));
	}

	/**
	 * @Description 修改 提供产品表
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param provideProduct
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("提供产品模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入provideProduct")
	@Log(title = "修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody ProvideProduct provideProduct) {
		return R.status(provideProductService.updateById(provideProduct));
	}

	/**
	 * @Description 新增或修改 提供产品表
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param provideProduct
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("提供产品模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入provideProduct")
	@Log(title = "新增或修改", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody ProvideProduct provideProduct) {
		return R.status(provideProductService.saveOrUpdate(provideProduct));
	}

	
	/**
	 * @Description 删除 提供产品表
	 * @Author daina
	 * @Date 20:52 2020/3/10
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("提供产品模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "逻辑删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(provideProductService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	 * @Description 修改时候删除图片
	 * @Author daina
	 * @Date 20:50 2020/3/10
	 * @param id
	 * @param busId
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/deleteImage")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "修改时候删除图片", notes = "id,busId")
	@Log(title = "修改时候删除图片", businessType = BusinessType.UPDATE)
	public R deleteImage(@ApiParam(value = "主键集合", required = true) @RequestParam Long id, @ApiParam(value = "主键集合", required = true) @RequestParam Long busId){
		if(pluploadService.selectById(busId) != null){
			pluploadService.fileDelete(busId);
		}
		ProvideProduct provideProduct = new ProvideProduct();
		provideProduct.setId(id);
		provideProduct.setImgId(-1L);
		return R.status(provideProductService.updateById(provideProduct));
	}
	
}
